import {
    getMenusByRouters,
    setTagNavListInLocalstorage,
    getTagNavListFromLocalstorage,
    getBreadCrumbList,
    getHomeRoute,
    routeHasExist
} from '@/libs/menus'
import routers from '@/router/BaseRouter'

export default {
    state: {
        // 侧边栏收起或展开
        collapseSidebar: false,
        // 标签导航
        tagNavList: [],
        // 面包屑导航
        breadCrumbList: [],
        breadCrumbHomeRoute: {
            name: 'index',
            to: '',
            meta:{title: '首页'}
        },
        homeRoute: null
    },

    getters: {
        collapseSidebar: state => state.collapseSidebar,
        menus: (state, getters, rootState) => {
            state.homeRoute = getHomeRoute(routers)
            return getMenusByRouters(routers, rootState.user.access, rootState.user.isAdmin)
        }
    },

    mutations: {
        /**
         * 侧边栏收起或展开
         */
        toggleCollapseSidebar: state => {
            state.collapseSidebar = !state.collapseSidebar
        },

        /**
         * 面包屑导航
         * @param {*} state
         * @param {*} routeMetched
         */
        setBreadCrumb(state, routeMetched) {
            state.breadCrumbList = getBreadCrumbList(routeMetched, state.breadCrumbHomeRoute)
        },

        /**
         * 导航标签保存
         * @param {*} state
         * @param {*} list
         */
        setTagNavList: (state, list) => {
            if (list) {
                let isHideInTags = list[list.length-1].meta.hideInTags
                if(!isHideInTags) {
                    state.tagNavList = [...list]
                    setTagNavListInLocalstorage([...list])
                }
            } else {
                state.tagNavList = getTagNavListFromLocalstorage()
            }
        },
        addTag(state, { route, type = 'unshift' }) {
            if (!routeHasExist(state.tagNavList, route)) {
                if (type === 'push') state.tagNavList.push(route)
                else {
                    if (route.name === 'console') state.tagNavList.unshift(route)
                    else state.tagNavList.splice(1, 0, route)
                }
                setTagNavListInLocalstorage([...state.tagNavList])
            }
        }


    },

    actions: {
    }
}
